import cv2
from utils.common import load_image, show_image, make_dirs  # 导入公共函数
from utils.config import exp2_gray_path, exp2_binary_path, exp2_adaptive_binary_path, exp2_output_dir  # 导入输出路径

# 确保输出目录存在
make_dirs(exp2_output_dir)

# 加载灰度图像
gray_image = cv2.imread(exp2_gray_path, cv2.IMREAD_GRAYSCALE)

# 全局二值化
_, binary_image = cv2.threshold(gray_image, 128, 255, cv2.THRESH_BINARY)
show_image("Binary Image", binary_image)

# 自适应二值化
adaptive_binary_image = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
show_image("Adaptive Binary Image", adaptive_binary_image)

# 保存结果
cv2.imwrite(exp2_binary_path, binary_image)
cv2.imwrite(exp2_adaptive_binary_path, adaptive_binary_image)
print(f"二值化结果已保存到：{exp2_binary_path}")
print(f"自适应二值化结果已保存到：{exp2_adaptive_binary_path}")